<template>
  <div>
    <!-- B端订单提报 -->
    <TopFilter  ref="topfrom" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="OrderSubmissionSideB"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :isSelectColumn="false"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(2)">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="addVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="80%">
      <addModel @close="addcloseHandler" />
    </Drawer>
    <Drawer :visible.sync="emmitVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="80%">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="80%">
      <detailModel :rowData="formData" @close="detailcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, exportList,deleteOrderById, exporturl } from '@/api/dealerSale/OrderManagement/OrderSubmissionSideB/index.js';
import addModel from './addModel';
import emitModel from './emitModel';
import detailModel from './detailModel';
import { exportExcelFile } from '@/utils';
import moment from 'moment';
export default {
  name: 'OrderSubmissionSideB',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        isDealer: 10041001
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      emmitVisible: false,
      detailVisible: false,
      addVisible: false
    };
  },
  mixins: [dictionary],
  components: {
    addModel,
    emitModel,
    detailModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    //默认查询条件
    this.fetchParams.orderYearMonth =  this.topFilterList.find((x) => x.fieldName == 'orderYearMonth').initialValue;
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'DATE',
          label: '订单年月',
          fieldName: 'orderYearMonth',
          initialValue: moment().format('YYYY-MM'),
          dateType: 'month'
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2417')
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('2105')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 150,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)} disabled={props.row.orderStatus != 24171001}>
                  编辑
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 3)} disabled={props.row.orderStatus != 24171001}>
                  取消
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2105'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '2105')}</span>;
          }
        },
        {
          title: '紧急度',
          dataIndex: 'emergencyType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2101'),
          render: p => {
            return <span>{this.createDictText(p.row.emergencyType, '2101')}</span>;
          }
        },
        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2417'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '2417')}</span>;
          }
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearAndMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2409'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          }
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '送达城市',
          dataIndex: 'deliveryCityName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系方式',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报数量',
          dataIndex: 'orderTotalNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.createdName}</span>;
          }
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
   async addHandle(type) {
      if (type == 1) {
        this.addVisible = true;
      } else {

      // let json = Object.assign({}, { CODE: 'CODE_0000034' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }

        this.$refs.topfrom.SUBMIT_FORM();

        this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
          confirmButtonText: '下载',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let json = this.fetchParams;
            this.exportLoading = true;

            exportList(json)
              .then(response => {
                exportExcelFile(response.data, 'B端订单提报');
                this.exportLoading = false;
              })
              .catch(err => {
                console.log(err);
              });
          })
          .catch(() => {
            console.log('456');
            this.$message({
              type: 'info',
              message: '已取消'
            });
          });
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async editHandle(data, type) {
      this.formData = data;
      if (type == 1) {
        this.emmitVisible = true;
      } else if (type == 2) {
        this.detailVisible = true;
      }
      else {
        await this.$confirm('是否确定取消?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let res = await deleteOrderById({ orderId: data.orderId });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '取消成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    emitcloseHandler(val) {
      this.emmitVisible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    addcloseHandler(val) {
      this.addVisible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    detailcloseHandler() {
      this.detailVisible = false;
    }
  }
};
</script>
